
package com.scs.application.modules.settlement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanValidators;
import com.scs.application.modules.settlement.dto.BatchDeleteDTO;
import com.scs.application.modules.settlement.dto.InvoiceItemDTO;
import com.scs.application.modules.settlement.entity.InvoiceItem;
import com.scs.application.modules.settlement.request.InvoiceItemQueryRequest;
import com.scs.application.modules.settlement.service.InvoiceItemService;
import com.scs.application.modules.settlement.vo.InvoiceItemVO;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 发票单明细 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/settlement/invoice_items")
public class InvoiceItemController extends CrudController<InvoiceItem, String, InvoiceItemService> implements ImAndExAbleController<InvoiceItemQueryRequest> {

    @GetMapping("/page")
    public PageResult page(InvoiceItemQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(InvoiceItemQueryRequest request) {
        List<InvoiceItem> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/list_for_view")
    public CommonResult listForView(InvoiceItemQueryRequest request) {
        List<InvoiceItemVO> list = service.listForView(request);
        return CommonResult.ok("查询成功", list);
    }

    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<InvoiceItem> children) {
        Assert.notEmpty(children, "至少有一条采购订单");
        BeanValidators.validateListWithException(validator, children);
        boolean bSuccess = service.saveOrUpdateBatch(children);
        return bSuccess ? CommonResult.ok("关联成功") : CommonResult.error("关联失败");
    }

    @GetMapping("/page_for_relate")
    public PageResult pageForRelate(InvoiceItemQueryRequest request) {
        IPage page = service.pageForRelate(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/page_for_view")
    public PageResult pageForView(InvoiceItemQueryRequest request) {
        IPage page = service.pageForView(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @PostMapping("/batch_delete")
    public CommonResult batchDelete(@RequestBody @Valid BatchDeleteDTO batchDeleteDTO) {
        service.batchDelete(batchDeleteDTO.getIds(),batchDeleteDTO.getInvoiceLineId());
        return CommonResult.ok("删除成功");
    }

    /**删除开票明细*/
    @PostMapping("/deleteInvoiceItem")
    public CommonResult deleteInvoiceItem(@RequestBody @Valid InvoiceItemDTO invoiceItemDTO) {
        Double invoiceAmount = service.deleteInvoiceItem(invoiceItemDTO);
        return CommonResult.ok("删除成功", invoiceAmount);
    }

}
